package com.info33.platform.business.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.info33.platform.business.app.dao.AppBannerDao;
import com.info33.platform.business.app.entity.AppBanner;
import com.info33.platform.business.app.service.AppBannerService;
import com.info33.platform.business.app.util.enmus.AppBannerEnum;
import com.info33.platform.common.constant.TableConstant;
import com.info33.platform.common.mvc.vo.Result;
import com.info33.platform.common.util.SysUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author admin
 */
@Service
public class AppBannerServiceImpl extends ServiceImpl<AppBannerDao, AppBanner> implements AppBannerService {

    @Autowired
    private AppBannerDao appBannerDao;

    @Override
    public IPage<AppBanner> pcBanner(Page<AppBanner> page, AppBannerEnum type, Long tid) {
        IPage<AppBanner> iPage = appBannerDao.selectPage(page, new QueryWrapper<AppBanner>()
                .select(TableConstant.ID, "img_url", "route", "common", TableConstant.UPDATE_DATE, TableConstant.REMARKS, TableConstant.SORT)
                .eq("type", type)
                .and(l -> l.eq("common", Boolean.TRUE)
                        .or()
                        .eq(TableConstant.TENANT_ID, tid))
                .orderByDesc("common")
                .orderByAsc(TableConstant.SORT)
                .orderByDesc(TableConstant.CREATE_DATE));
        long count = iPage.getRecords().stream().filter(l -> l.getCommon().equals(Boolean.TRUE)).count();
        if (count < iPage.getTotal()) {
            for (long i = 0; i < count; ) {
                iPage.getRecords().remove(Integer.parseInt(i + ""));
                count--;
            }
        }
        return iPage;
    }

    @Override
    public List<AppBanner> pcBanner(AppBannerEnum type, Long tid) {
        List<AppBanner> list = appBannerDao.selectList(new QueryWrapper<AppBanner>()
                .select("img_url", "route", "common")
                .eq("type", type)
                .and(l -> l.eq("common", Boolean.TRUE)
                        .or()
                        .eq(TableConstant.TENANT_ID, tid))
                .orderByAsc(TableConstant.TENANT_ID));
        long count = list.stream().filter(l -> l.getCommon().equals(Boolean.TRUE)).count();
        if (count < list.size()) {
            for (long i = 0; i < count; ) {
                list.remove(Integer.parseInt(i + ""));
                count--;
            }
        }
        return list;
    }

    @Override
    public Result<Object> pcEdit(AppBanner appBanner) {
        AppBanner old = appBannerDao.selectById(appBanner.getId());
        if (Boolean.TRUE.equals(old.getCommon())
                && Boolean.FALSE.equals(SysUtil.isSysTenantAdmin())) {
            return Result.error(Result.RESULT_ERROR_NO_POWER);
        }
        return this.updateById(appBanner) ? Result.ok() : Result.error();
    }

    @Override
    public Result<Object> pcRemove(Long id) {
        AppBanner old = appBannerDao.selectById(id);
        if (Boolean.TRUE.equals(old.getCommon())
                && Boolean.FALSE.equals(SysUtil.isSysTenantAdmin())) {
            return Result.error(Result.RESULT_ERROR_NO_POWER);
        }
        return this.removeById(id) ? Result.ok() : Result.error();
    }
}